package com.zhiwang.planet.util

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import com.zhiwang.planet.App
import com.zhiwang.planet.mvvm.model.StringHolder
import com.zhiwang.planet.mvvm.model.entity.ShareInformation
import com.zhiwang.planet.util.walle_channel.reader.WalleChannelReader

@SuppressLint("ApplySharedPref")
class TokenManager {

    companion object {
        lateinit var context: Application

        var channel: String = ""

        fun init(context: App) {
            Companion.context = context
        }

        fun isLoginValid(): Boolean {
            val token = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
                    .getString("Token", "")

            return !TextUtils.isEmpty(token)
        }


        fun saveToken(it: String?) {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            preferences
                    .edit()
                    .putString("Token", it)
                    .commit()
        }


        fun savePhone(phone: String) {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            preferences
                    .edit()
                    .putString("Phone", phone)
                    .commit()
        }

        fun hasSetPhone(): Boolean {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            return preferences.getString("Phone", "")?.isEmpty() != true
        }

        fun clearPhone() {
            context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
                    .edit()
                    .remove("Phone")
                    .commit()
        }


        fun clearToken() {
            StringHolder.sdkUrl =""
            //清除token
            context.getSharedPreferences("DATA", Context.MODE_PRIVATE).edit()
                    .clear().commit()
        }

        fun token(): String {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            return preferences.getString("Token", "")!!
        }


        fun saveDevice(deviceId: String) {
            val preferences: SharedPreferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            preferences.edit().putString("deviceId", deviceId).commit()
        }

        fun getDeviceId() = context.getSharedPreferences("DATA", Context.MODE_PRIVATE).getString("deviceId", "")

        fun webviewHeaderToken(): String {
            val t = token()
            if (t.isEmpty())
                return "emptytoken13anerfi"//使用一个无效的token
            else return t
        }

        fun isFirstTimeInstall(): Boolean {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)
            return TextUtils.isEmpty(preferences.getString("KEY_EVER_INSTALLED", ""))
        }


        fun setPhoneLogged() {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)
            preferences.edit().putBoolean("PhoneLogged", true).commit()
        }

        fun clearPhoneLogged() {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)
            preferences.edit().putBoolean("PhoneLogged", false).commit()
        }

        fun hasPhoneLogged() = context
                .getSharedPreferences("Install_Data", Context.MODE_PRIVATE)
                .getBoolean("PhoneLogged", false)


        fun install() {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)
            preferences.edit()
                    .putString("KEY_EVER_INSTALLED", "notfirstInstalled")
                    .apply()
        }


        fun saveUserid(userid: String) {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            preferences
                    .edit()
                    .putString("Userid", userid)
                    .commit()
        }

        fun webviewHeaderUserid(): String {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            return preferences.getString("Userid", "")!!
        }


        fun saveShareInfo(title: String, summary: String, img: String, url: String) {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            preferences.edit()
                    .putString("shareTitle", title)
                    .putString("shareSummary", summary)
                    .putString("shareImg", img)
                    .putString("shareUrl", url)
                    .commit()
        }

        fun getShareInfo(): ShareInformation {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)

            val title = preferences.getString("shareTitle", "oak")
            val summary = preferences.getString("shareSummary", "aoksummary")
            val img = preferences.getString("shareImg", "https://preview.qiantucdn.com/58pic/36/76/61/14r58PICG852b45JYUR6W_PIC2018.jpg!qt324new_nowater")
            val url = preferences.getString("shareUrl", "http://xq.ji7c.com/invite/redbag_android?inviteuid=803525\n")
            return ShareInformation(title ?: "", summary ?: "", img ?: "", url ?: "")
        }

        fun serviceQQ(): String {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)
            return preferences.getString("QQ", "")!!
        }


        fun saveServiceQQ(qq: String) {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)

            preferences.edit()
                    .putString("QQ", qq)
                    .commit()
        }

        fun hasShowGuide(): Boolean {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)

            return preferences.getBoolean("HasGuided", false)
        }


        fun setHasShowGuide() {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)

            if (preferences.getBoolean("HasGuided", false)) return

            preferences.edit()
                    .putBoolean("HasGuided", true)
                    .commit()
        }


        fun saveMiniGameAccount(accountToken: String) {
            context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
                    .edit()
                    .putString("ByteDanceTinyGameAccount", accountToken)
                    .commit()
        }

        fun miniGameAccount(): String {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)

            return preferences.getString("ByteDanceTinyGameAccount", "") ?: ""
        }


        fun syncMiniGame() {
            context.getSharedPreferences("DATA", Context.MODE_PRIVATE).edit().putBoolean("TinyGameAccountSync", true).commit()
        }

        fun hasMiniGameAccount(): Boolean {
            return context.getSharedPreferences("DATA", Context.MODE_PRIVATE).getBoolean("TinyGameAccountSync", false)
        }

        fun saveQQKey(key: String) {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)

            preferences.edit()
                    .putString("QQ_KEY", key)
                    .commit()
        }

        fun qqKey(): String {

            return context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE).getString("QQ_KEY", "")
        }


        fun channel(): String {

            return WalleChannelReader.getChannel(context) ?: ""
//
//            return when {
//                TextUtils.isEmpty(channel).not() -> {
//                    channel
//                }
//                else -> {
//
//                    channel = WalleChannelReader.getChannel(context) ?: ""
//                    channel
//                }
//            }
        }

        fun saveAndroid10Oaid(oaid: String) {
            context
                    .getSharedPreferences("DATA", Context.MODE_PRIVATE)
                    .edit()
                    .putString("Oaid", oaid)
                    .commit()

        }

        fun oaid(): String = context
                .getSharedPreferences("DATA", Context.MODE_PRIVATE)
                .getString("Oaid", "")
    }
}